<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
      @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="年度" name="year">
            <t-select style="width: 240px;" clearable v-model="queryParams.year">
              <t-option v-for="(item, index) of yearList" :key="index" :label="item.value" :value="item.key">{{
                item.value }}</t-option>
            </t-select>
          </t-form-item>
          <t-form-item label="月度" name="month">
            <t-select style="width: 240px;" clearable v-model="queryParams.month">
              <t-option v-for="(item, index) of monthList" :key="index" :label="item.value" :value="item.key">{{
                item.value }}</t-option>
            </t-select>
          </t-form-item>
          <t-form-item label="调度表名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入调度表名称" style="width: 240px" />
          </t-form-item>
          <t-form-item label="填报单位" name="deptName">
            <t-input v-model="queryParams.deptName" clearable placeholder="请输入填报单位" style="width: 240px" />
          </t-form-item>
          <t-form-item label="归口管理单位" name="manageDeptName">
            <t-input v-model="queryParams.manageDeptName" clearable placeholder="请输入归口管理单位" style="width: 240px" />
          </t-form-item>
          <t-form-item label="审批状态" name="approveStatus">
            <t-select style="width: 240px" clearable v-model="queryParams.approveStatus"
              :options="dict.type.approve_status">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination" :loading="dataLoading">
      <template #approveStatus="{ row }">
        <t-tag v-if="row.approveStatus" @click="dialog.header = row.name; dialog.objId = row.id; dialog.open_approve_list = true" class="cursor-pointer" :theme="handleTheme(row.approveStatus, dict.type.approve_status )" variant="light">
               {{ row.approveStatus | filterByDict(dict.type.approve_status) }}
        </t-tag>
      </template>
      <template #op="{ row }">
        <t-button size="small"
          @click="dialog.objId = row.id; dialog.header = row.name; dialog.open_detail = true"
          variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
        <!-- <t-button theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button> -->
      </template>
    </t-table>
    <dispatch-detail-dialog :objId="dialog.objId" :header="dialog.header" :visible.sync="dialog.open_detail" v-if="dialog.open_detail">
    </dispatch-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>
  </div>
</template>

<script>
import DispatchDetailDialog from "./components/dispatch-detail-dialog.vue";
import ApproveList from "../approve/components/approve-list.vue";

export default {
  name: "dispatch-list",
  dicts: ['approve_status'],
  components: {
    DispatchDetailDialog,
    ApproveList
  },
  data() {
    return {
      dialog: {
        header: "",
        open: false,
        open_detail: false,
        objId: null,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      data: [],
      monthList: [
        { key: 1, value: "1月" },
        { key: 2, value: "2月" },
        { key: 3, value: "3月" },
        { key: 4, value: "4月" },
        { key: 5, value: "5月" },
        { key: 6, value: "6月" },
        { key: 7, value: "7月" },
        { key: 8, value: "8月" },
        { key: 9, value: "9月" },
        { key: 10, value: "10月" },
        { key: 11, value: "11月" },
        { key: 12, value: "12月" }
      ],
      yearList: [],
      columns: [
        {
          colKey: "year",
          title: "年度",
          align: "center"
        },
        {
          colKey: "month",
          title: "月度",
          align: "center"
        },
        {
          colKey: "name",
          title: "调度表名称",
          align: "center"
        },
        {
          colKey: "deptName",
          title: "填报单位",
          align: "center"
        },
        {
          colKey: "manageDeptName",
          title: "归口管理单位",
          align: "center"
        },
        {
          colKey: "approveStatus",
          title: "审批状态",
          align: "center"
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 140,
          fixed: 'right'
        }
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    };
  },
  created() {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query))
    const year = new Date().getFullYear();
    for (let i = year; i >= year - 15; i--) {
      this.yearList.push({ key: i, value: `${i}年` });
    }
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.cgzh.dispatch.list(this.queryParams).then(response => {
        this.data = response.rows;
        this.pagination.total = response.total;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    delObj(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除调度表名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          this.$api.cgzh.dispatch.delete(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
};
</script>

<style scoped></style>
